#----------------------------------*-sh-*--------------------------------------
# =========                 |
# \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
#  \\    /   O peration     |
#   \\  /    A nd           | www.openfoam.com
#    \\/     M anipulation  |
#------------------------------------------------------------------------------
#     Copyright (C) 2011-2016 OpenFOAM Foundation
#     Copyright (C) 2016-2020 OpenCFD Ltd.
#------------------------------------------------------------------------------
# License
#     This file is part of OpenFOAM, distributed under GPL-3.0-or-later.
#
# File
#     etc/config.sh/paraview
#     - sourced by OpenFOAM-*/etc/bashrc or via foamPV alias
#
# Description
#     Setup for PARAVIEW (partially QT too)
#
#     To disable use of the PV_PLUGIN_PATH:
#         ParaView_VERSION=none
#
#     For system-wide installations:
#         cmake_version=cmake-system
#         ParaView_QT=qt-system
#         ParaView_VERSION=system       [EXPERIMENTAL]
#
# Note
#     The following env. variables are required for building plugins:
#         ParaView_DIR
#         PV_PLUGIN_PATH
#     The OpenFOAM plugin must be the first in PV_PLUGIN_PATH and have
#     paraview-major.minor encoded in its name.
#
#     For central installations not located under ThirdParty, the environment
#     values must be set manually. For example,
#
#         export ParaView_DIR=/opt/paraview/paraview-5.6.0
#         export PV_PLUGIN_PATH=$FOAM_LIBBIN/paraview-5.6
#
#         export PATH=$ParaView_DIR/bin:$PATH
#         export LD_LIBRARY_PATH=$ParaView_DIR/lib:$LD_LIBRARY_PATH
#         unset ParaView_VERSION        # skip ThirdParty settings
#
#     In paraview-5.4 and earlier, the libraries were in the sub-directory:
#
#         export LD_LIBRARY_PATH=$ParaView_DIR/lib/paraview-5.4:$LD_LIBRARY_PATH
#
# Note
#     When _foamAddLib is unset (eg, called from makeParaView or from foamPV):
#       - the ParaView_VERSION variable is retained.
#
# Additional Notes
#     PV_PLUGIN_PATH is *only* set if the directory actually exists.
#
#------------------------------------------------------------------------------
# USER EDITABLE PART: Changes made here may be lost with the next upgrade

ParaView_VERSION=5.6.3
ParaView_QT=qt-system

# END OF (NORMAL) USER EDITABLE PART
#------------------------------------------------------------------------------
pv_api=auto             # Either auto or pair of (major.minor) digits

# Compiler-specific location for ThirdParty installations
archDir="$WM_THIRD_PARTY_DIR/platforms/$WM_ARCH$WM_COMPILER"

# Clean path and library path
eval \
    "$($WM_PROJECT_DIR/bin/foamCleanPath -sh-env=PATH \
    $ParaView_DIR $archDir/ParaView- $archDir/qt-)"

eval \
    "$($WM_PROJECT_DIR/bin/foamCleanPath -sh-env=LD_LIBRARY_PATH \
    $ParaView_DIR $archDir/ParaView- $archDir/qt-)"

# Evaluate command-line parameters for ParaView
for i
do
    case "$i" in
    (ParaView*=*)
        # name=value  -> export name=value
        eval "export $i"
        ;;
    esac
done

case "$ParaView_VERSION" in
('')
    # empty - do nothing
    ;;

(none)
    unset ParaView_DIR PV_PLUGIN_PATH

    if [ -n "$FOAM_VERBOSE" ] && [ -n "$PS1" ]
    then
        echo "Using paraview (disabled)" 1>&2
    fi
    ;;

(system)
    unset ParaView_DIR PV_PLUGIN_PATH
    eval "$($WM_PROJECT_DIR/bin/foamEtcFile -sh -config paraview-system)"
    ;;

(*)
    # ParaView_VERSION set and non-empty
    unset ParaView_DIR PV_PLUGIN_PATH

    # Set API to correspond to VERSION
    # pv_api is <digits>.<digits> from ParaView_VERSION
    case "$ParaView_VERSION" in
    ("$pv_api".*)
        # API and VERSION appear to correspond
        ;;

    ([0-9]*)
        # Extract API from VERSION
        pv_api=$(echo "$ParaView_VERSION" | \
            sed -ne 's/^[^0-9]*\([0-9][0-9]*\.[0-9][0-9]*\).*$/\1/p')
        ;;
    esac

    export ParaView_DIR="$archDir/ParaView-$ParaView_VERSION"
    pv_plugin_dir="$FOAM_LIBBIN/paraview-$pv_api"

    # Set paths if binaries are present
    if [ -r "$ParaView_DIR" ]
    then
        export PATH="$ParaView_DIR/bin:$PATH"

        # QT libraries as required, and Qt5_DIR for the root directory.
        # Another possibility: "qtpaths --qt-version"
        qtDir="$archDir/$ParaView_QT"
        if [ -d "$qtDir" ]
        then
            case "$ParaView_QT" in
            (*-5*)
                export Qt5_DIR="$qtDir"
                ;;
            esac
            for libDir in "lib$WM_COMPILER_LIB_ARCH" "lib"
            do
                if [ -d "$qtDir/$libDir" ]
                then
                    export LD_LIBRARY_PATH="$qtDir/$libDir:$LD_LIBRARY_PATH"
                    break
                fi
            done
        fi

        # ParaView libraries
        # - 5.4 and earlier: lib/paraview-X.X
        # - 5.5 and later:   lib/, but could also be lib64/
        for libDir in "lib$WM_COMPILER_LIB_ARCH" "lib"
        do
            pvLibDir="$libDir/paraview-$pv_api"
            if [ -d "$ParaView_DIR/$pvLibDir" ]
            then
                case "$pv_api" in
                (5.[0-4]*)
                    libDir="$pvLibDir"   # Needs lib/paraview-X.X (not lib)
                    ;;
                esac
                export LD_LIBRARY_PATH="$ParaView_DIR/$libDir:$LD_LIBRARY_PATH"
                break
            fi
            unset pvLibDir
        done

        # OpenFOAM plugin directory must be the first in PV_PLUGIN_PATH
        # and have paraview-major.minor encoded in its name
        if [ -d "$pv_plugin_dir" ]
        then
            export PV_PLUGIN_PATH="$pv_plugin_dir"
        else
            pv_plugin_dir="${pv_plugin_dir} (missing)"  # For message
        fi

        if [ -n "$FOAM_VERBOSE" ] && [ -n "$PS1" ]
        then
            echo "Using paraview" 1>&2
            echo "    ParaView_DIR    : $ParaView_DIR" 1>&2
            echo "    library         : $pvLibDir" 1>&2
            echo "    plugins         : $pv_plugin_dir" 1>&2
        fi
    else
        if [ -n "$FOAM_VERBOSE" ] && [ -n "$PS1" ]
        then
            echo "No paraview found" 1>&2
            echo "    ParaView_DIR         : $ParaView_DIR" 1>&2
        fi

        unset ParaView_DIR
    fi
    ;;
esac


#------------------------------------------------------------------------------

if command -v _foamAddLib >/dev/null  # normal sourcing
then
    unset ParaView_VERSION ParaView_QT
fi

unset archDir libDir
unset pv_api pv_plugin_dir pvLibDir pvPython qtDir

#------------------------------------------------------------------------------
